<template>
  <div style="position: relative;">
    <el-form :inline="true" size="mini" :model="formInline" label-position="right">
      <el-form-item label="项目名称">
        <el-input class="filter-item" v-model="formInline.projectName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="公司名称">
        <el-input class="filter-item" v-model="formInline.companyName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="缴纳日期">
        <el-date-picker v-model="times" type="daterange" align="right" unlink-panels
          format="yyyy-MM-dd" value-format="yyyy-MM-dd"
          range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
          :picker-options="pickerOptions" @change="selecttime">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="业务类型">
        <el-cascader v-model="formInline.businessType"
                     ref="ref_business"
                     :options="businesstypeTree"
                     :props="businesstProps"
                     :show-all-levels="false"
                     clearable></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit">搜索</el-button>
        <el-button type="warning" class="el-button--reset" icon="el-icon-refresh-right" @click="resetFromdata" size="mini"></el-button>
        <el-button type="primary" size="mini" @click="addFromdata">创建</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="24">
        <el-table :data="tableData" size="mini" style="width: 100%;" header-cell-class-name="table-header-gray" v-loading="isShowloading">
          <el-table-column type="index" align="center" width="55" label="序号"></el-table-column>
          <el-table-column prop="projectName" align="center" label="项目名称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="companyName" align="center" label="公司名称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="businessTypeName" align="center" label="业务类型" show-overflow-tooltip></el-table-column>
          <el-table-column prop="amount" align="center" label="缴纳金额（元）" show-overflow-tooltip></el-table-column>
          <el-table-column prop="paymentDate" align="center" label="缴纳时间" show-overflow-tooltip></el-table-column>
          <el-table-column prop="createTime" align="center" label="创建时间" show-overflow-tooltip></el-table-column>
          <el-table-column prop="updateTime" align="center" label="上次修改日期" width="120"></el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="200">
            <template slot-scope="scope">
              <el-button type="text" @click="handleInfo(scope.row)">详情</el-button>
              <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="end" style="margin-top: 10px;">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNo"
            :page-size="page_size"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            :total="total"></el-pagination>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { Securitydeposit } from '@/api/projectAdministration.js';
import Securitydepositadd from './Securitydepositadd.vue';
import SecuritydepositDetail from './SecuritydepositDetail.vue';
import { Businesstype } from '@/api/business.js';
export default {
  name: 'Securitydeposit',
  data () {
    return {
      editDialogVisible: false,
      businesstypeTree: [],
      businesstypeId: [],
      businesstProps: {
        value: 'id',
        label: 'businessName',
        children: 'children',
        emitPath: false
      },
      formInline: {
        projectName: null,
        companyName: null,
        paymentDateFrom: null,
        paymentDateTo: null,
        businessType: null
      },
      showDetail: {},
      showEdit: {},
      tableData: [],
      villageData: [],
      total: 0,
      page_size: 10,
      pageNo: 1,
      isShowloading: false,
      Addroptions: [],
      defaultProps: {
        children: 'nodes',
        label: 'name'
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      times: ''
    }
  },
  created () {
    this.loadbusinesstypeTree()
    this.loadDepositWidgetData()
  },
  activated () {
    this.loadDepositWidgetData()// 数据加载的方法
  },
  computed: {},
  methods: {
    // 选择签约日期
    selecttime (times) {
      if (times) {
        this.formInline.paymentDateFrom = times[0]
        this.formInline.paymentDateTo = times[1]
      } else {
        this.formInline.paymentDateFrom = ''
        this.formInline.paymentDateTo = ''
      }
    },
    /**
     * 业务类型信息表树形列表
     */
    loadbusinesstypeTree () {
      return new Promise((resolve, reject) => {
        Businesstype.tree(this).then(res => {
          this.businesstypeTree = res.data
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 保证金管理
     */
    loadDepositWidgetData () {
      let params = {
        ...this.formInline,
        size: this.page_size,
        current: this.pageNo
      }
      return new Promise((resolve, reject) => {
        Securitydeposit.list(this, params).then(res => {
          this.tableData = res.data.records
          this.total = res.data.total
        }).catch(e => {
          reject(e);
        });
      });
    },
    // 搜索
    onSubmit () {
      this.pageNo = 1
      this.loadDepositWidgetData()
    },
    // 重置
    resetFromdata () {
      this.formInline = {
        projectName: null,
        companyName: null,
        paymentDateFrom: null,
        paymentDateTo: null,
        businessType: null
      }
      this.times = ''
      this.loadDepositWidgetData()
    },
    // 分页导航
    handleCurrentChange (val) {
      this.pageNo = val
      this.loadDepositWidgetData()
    },
    handleSizeChange (val) {
      this.page_size = val
      this.loadDepositWidgetData()
    },
    currentChange (val) {
      this.pageNo = val
      this.loadDepositWidgetData()
    },
    // 创建
    addFromdata () {
      let params = {};
      this.$dialog.show('新建', Securitydepositadd, {
        area: '800px'
      }, params).then(res => {
        this.loadDepositWidgetData();
      }).catch(e => {});
    },
    // 查看
    handleInfo (row) {
      let params = {
        depositId: row.id
      }
      this.$dialog.show('详情', SecuritydepositDetail, {
        area: '800px'
      }, params).then(res => {
        this.loadDepositWidgetData()
      }).catch(e => {
      });
    },
    // 删除
    handleDelete (row) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          id: row.id
        };
        return new Promise((resolve, reject) => {
          Securitydeposit.delete(this, params).then(res => {
            this.loadDepositWidgetData()
          }).catch(e => {
            reject(e);
          });
        });
      })
    }
  }
}
</script>
